package cn.edu.bupt.freewings.forum.front.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.bupt.freewings.forum.front.utility.CommonUtility;
import cn.edu.bupt.freewings.forum.front.utility.TypeBuilder;
import cn.edu.bupt.freewings.forum.front.beans.UserBean;
import cn.edu.bupt.freewings.forum.client.rest.UserClient;
import cn.edu.bupt.freewings.forum.common.UserType;

/**
 * @author hubaiyu (hubaiyu@gmail.com)
 *
 */
public class NewUserAction extends HttpServlet {
	public static final String PARAM_NAME = "name";
	public static final String PARAM_NICK_NAME = "nickName";
	public static final String PARAM_PASSWORD = "password";
	
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		/*UserBean user = CommonUtility.getUser(request, response);
		if(user == null || user.getId() != 1){
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
		}*/
		
		String name = (String)request.getParameter(PARAM_NAME);
		String nickName = (String)request.getParameter(PARAM_NICK_NAME);
		String password = (String)request.getParameter(PARAM_PASSWORD);
		UserType.User newUser = TypeBuilder.makeUser(name, nickName, password);
		boolean result = new UserClient().put(newUser);
		//TODO: Add regist fail logic
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}
}